package client

import (
	"chess_platform/internal/bitmap"
	. "chess_platform/internal/common"
	"fmt"
)

//主要写一些活动的

func IsSet(uid int64, bit int) (bool, error) {
	reward, err := RewardById(uid)
	if err != nil {
		return false, err
	}
	return bitmap.IsSet(reward, bit)
}

func SetBit(uid int64, bit int) error {
	reward, err := RewardById(uid)
	if err != nil {
		return err
	}
	reward, err = bitmap.SetBit(reward, bit)
	if err != nil {
		return err
	}
	data := map[string]interface{}{
		"reward": reward,
	}
	_, err = UpdateUser(data, uid)
	return err
}

/*
	uid:表示哪个用户
	lastLoginTime :表示上次登录时间
	bit:表示消除哪位
*/
//暂时是判断是隔天的
func ClearBit(uid, lastLoginTime int64, bit int) error {
	if !IsToday(lastLoginTime) {
		return clearBit(uid, bit)
	}
	return nil
}

func clearBit(uid int64, bit int) error {
	reward, err := RewardById(uid)
	if err != nil {
		return err
	}
	reward, err = bitmap.Clear(reward, bit)
	if err != nil {
		return err
	}
	data := map[string]interface{}{
		"reward": reward,
	}
	_, err = UpdateUser(data, uid)
	return err
}

func RewardById(uid int64) (string, error) {
	sql := fmt.Sprintf(`
		SELECT
			reward
		FROM
			%v
		WHERE
			uid = %v
	`, TableClientUser, uid)
	o := NewDBOrm(DBChess)
	var reward string
	err := o.Raw(sql).QueryRow(&reward)
	return reward, err
}
